<?php
class TaskAction extends BaseAction
{
    function _initialize()
    {
        parent::_initialize();
        $this->assign('nav', 2);
    }

    public function index()
    {
        $this->display();
    }

    public function check()
    {
        $this->display();
    }

    public function designPassing()
    {
        $map = array();
        $year = intval(getGet('year'));
        $month = intval(getGet('month'));
        if ($year && $month) {

        } else {
            $month = date('m');
            $year = date('Y');
        }
        $UserModel = D('User');
        $TaskAllotModel = D('TaskAllot');
        $TaskAuditReturnModel = D('TaskAuditReturn');
        $users = $TaskAllotModel->getAllStatisics($year, $month);
        foreach ($users as $key => $user) {
            $userReturn = $TaskAuditReturnModel->getUidCount($user['uid'], $year, $month);
            $users[$key]['passing'] = ($user['all_count'] / ($user['all_count'] + $userReturn['return_count'])) * 100;
            $users[$key]['all_return_count'] = $userReturn['return_count'];
            $users[$key]['num'] = $UserModel->getNum($user['uid']);
        }
        $this->assign('years', $this->getYears($year));
        $this->assign('months', $this->getMonths($month));
        $this->assign('users', $users);
        $this->display();
    }

    /**
     * 编辑和商场添加商场活动的总时间
     * @author ice<iceinto@mallog.com.cn>
     */
    public function editActivityTime()
    {
        $Date = new Date();
        $year = intval(getGet('year'));
        $month = intval(getGet('month'));
        if ($year && $month) {
            $Date = new Date($year . '-' . $month . '-1');
            $this->assign('month', $month);
        } else {
            $this->assign('month', date('n'));
        }
        $fireTime = $Date->firstDayOfMonth()->format("%Y-%m-%d");
        $lastTime = $Date->lastDayOfMonth()->format("%Y-%m-%d");
        $fireTime = strtotime($fireTime);
        $lastTime = strtotime($lastTime);
        //统计维护任务
        $map['create_time'] = array(array('gt', $fireTime), array('lt', $lastTime));
        $TaskModel = M('Task');
        $UserModel = D('User');
        $tasks = $TaskModel->field('sum(end_time - start_time) as all_time,uid')->where($map)->group('uid')->select();
        $dayAllCount = 0;
        foreach ($tasks as $key => $task) {
            $allTime = intval($task['all_time']);
            $dayCount = $allTime / 3600 / 24;
            $tasks[$key]['day_count'] = $dayCount;
            $dayAllCount += $dayCount;
            $tasks[$key]['num'] = $UserModel->getNum($task['uid']);
        }
        //获取添加电话号码
        unset($map);
        $map['add_time'] = array(array('gt', $fireTime), array('lt', $lastTime));
        $phones = M('MallPhone')->field('editor,count(1) as all_count')->where($map)->group('editor')->select();
        foreach ($phones as $key => $phone) {
            $phones[$key]['num'] = $UserModel->getNum($phone['editor']);
        }
        $this->assign('years', $this->getYears($year));
        $this->assign('months', $this->getMonths($month));
        $this->assign('day_all_count', $dayAllCount);
        $this->assign('tasks', $tasks);
        $this->assign('phones', $phones);
        $this->display();
    }

    /**
     * 品牌资讯排行
     */
    public function brand()
    {
        $TaskModel = M('Task');
        $brands = $TaskModel->field('bid,count(1) as bcount')->group('bid')->order('bcount DESC')->select();
        foreach ($brands as $key => $brand) {

        }
        $this->assign('brands', $brands);
        $this->display();
    }
}